package com.zzzzzz.content;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.zzzzzz.common.id.UUIDGenerator;
import com.zzzzzz.common.util.ImageUtils;
import com.zzzzzz.core.data.filestore.FileData;
import com.zzzzzz.core.data.filestore.FileStorage;

@Service("contentPictureService")
public class FileStorageContentPictureService implements ContentPictureService {

	@Resource
	private FileStorage storage;
	
	public String saveArticleCoverPicture(CommonsMultipartFile files) throws IOException {
		byte[] bytes = files.getBytes();
		assertBytesLength(bytes);
		String contentType = files.getContentType();
		assertContentType(contentType);
		String extension = FilenameUtils.getExtension(files.getOriginalFilename());
		
		String id = UUIDGenerator.get();
		FileData fileData = new FileData("articlecover/n/" + id + "." + extension, ImageUtils.scaleImage(bytes, extension, 600, 800), contentType);
		storage.storeFile(new FileData("articlecover/s/" + id + "." + extension, ImageUtils.scaleImage(bytes, extension, 170, 370), contentType));
		storage.storeFile(fileData);
		storage.storeFile(new FileData("articlecover/l/" + id + "." + extension, ImageUtils.scaleImage(bytes, extension, 1920, 3000), contentType));
		return fileData.getName();
	}
	
	public String saveArticlePicture(CommonsMultipartFile files) throws IOException {
		byte[] bytes = files.getBytes();
		assertBytesLength(bytes);
		String contentType = files.getContentType();
		assertContentType(contentType);
		String extension = FilenameUtils.getExtension(files.getOriginalFilename());
		
		String id = UUIDGenerator.get();
		FileData fileData = new FileData("article/n/" + id + "." + extension, ImageUtils.scaleImage(bytes, extension, 600, 800), contentType);
		storage.storeFile(fileData);
		storage.storeFile(new FileData("article/l/" + id + "." + extension, ImageUtils.scaleImage(bytes, extension, 1920, 3000), contentType));
		
		return fileData.getName();
	}
	
	public String savePicItemPicture(Long id, CommonsMultipartFile files) throws IOException {
		byte[] bytes = files.getBytes();
		assertBytesLength(bytes);
		String contentType = files.getContentType();
		assertContentType(contentType);
		String extension = FilenameUtils.getExtension(files.getOriginalFilename());
		
		FileData fileData = new FileData("picitem/n/" + id + "." + extension, ImageUtils.scaleImage(bytes, extension, 600, 800), contentType);
		storage.storeFile(fileData);
		storage.storeFile(new FileData("picitem/l/" + id + "." + extension, ImageUtils.scaleImage(bytes, extension, 1920, 3000), contentType));
		
		return fileData.getName();
	}

	private void assertBytesLength(byte[] imageBytes) {
		int length = imageBytes.length;
		if (length == 0) {
			throw new IllegalArgumentException("Cannot accept empty picture byte[] as a profile picture");
		} else if(length > 3145728){
			throw new MaxUploadSizeExceededException(3145728);
		}
	}

	private void assertContentType(String contentType) {
		if (!IMAGE_TYPES.contains(contentType)) {
			throw new IllegalArgumentException("Cannot accept content type '" + contentType + "' as a profile picture.");
		}
	}

	private static final List<String> IMAGE_TYPES = Arrays.asList("image/jpeg", "image/gif", "image/png", "application/octet-stream");
	
}